package com.zjgsu.nftplatform.controller;

import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.IpfsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ahy231
 * @date 2022/3/9 14:16
 * @description
 */
@Api(tags = "IPFS")
@RestController
@RequestMapping("/ipfs")
public class IpfsController {
    private final IpfsService ipfsService;

    public IpfsController(IpfsService ipfsService) {
        this.ipfsService = ipfsService;
    }

    @GetMapping("/cat")
    public Result cat(@ApiParam("文件哈希值") @RequestParam String hash) {
        try {
            return new Result(ResultCode.SUCCESS, ipfsService.cat(hash));
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @GetMapping("/get")
    public Result get(@ApiParam("文件哈希值") @RequestParam String hash) {
        try {
            return new Result(ResultCode.SUCCESS, ipfsService.get(hash));
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @PostMapping("/add")
    public Result add(@ApiParam("文件内容") @RequestBody MultipartFile file) {
        try {
            return new Result(ResultCode.SUCCESS, ipfsService.add(file));
        } catch (Exception e) {
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
